﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.ComponentModel;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Gama.Extension
{
    public static partial class HtmlHelperExtensions
    {
        public static ValidationMessageBuilder<TModel> ValidationMessageEx<TModel>(this HtmlHelper htmlHelper, Expression<Func<TModel, object>> expression)
        {
            return new ValidationMessageBuilder<TModel>(htmlHelper, expression);
        }
    }

    public class ValidationMessageBuilder<TModel> : HtmlInputElementBuilder<TModel>
    {
        private string message;

        internal ValidationMessageBuilder(HtmlHelper htmlHelper, Expression<Func<TModel, object>> expression)
            : base(htmlHelper, expression)
        {
        }

        internal ValidationMessageBuilder(HtmlInputElementBuilder<TModel> target)
            : base(target.HtmlHelper, target.Expression)
        {
        }

        protected override string BuildCore()
        {
            var htmlString = this.htmlHelper.ValidationMessage(this.htmlHelper.BuildBindingPath(expression), this.message, this.htmlAttributes);
            return htmlString == null
                    ? string.Empty
                    : htmlString.ToHtmlString();
        }

        public ValidationMessageBuilder<TModel> Message(string message)
        {
            this.message = message;

            return this;
        }   
    }
}
